package com.zeng.sim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeng.sim.controller.domain.SimMsgCodeMapping;
import com.zeng.sim.controller.domain.request.PageDomain;
import com.zeng.sim.mapper.SimMsgCodeMappingMapper;
import com.zeng.sim.mapper.model.SimMsgCodeMappingEntity;
import com.zeng.sim.service.SimMsgCodeMappingService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zengwanlin
 * @since 2023-12-27
 */
@Service
public class SimMsgCodeMappingServiceImpl extends ServiceImpl<SimMsgCodeMappingMapper, SimMsgCodeMappingEntity>
        implements SimMsgCodeMappingService {

    /**
     * 服务端报文映射列表查询
     * @param msgCodeMapping
     * @param pageDomain
     * @return
     */
    @Override
    public Page<SimMsgCodeMapping> data(SimMsgCodeMapping msgCodeMapping, PageDomain pageDomain) {
        Page page = Page.of(pageDomain.getPage(), pageDomain.getLimit());
        LambdaQueryWrapper<SimMsgCodeMappingEntity> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(msgCodeMapping.getReqChannelNo())) {
            wrapper.eq(SimMsgCodeMappingEntity::getReqChannelNo, msgCodeMapping.getReqChannelNo());
        }
        if (StringUtils.hasLength(msgCodeMapping.getReqMsgName())) {
            wrapper.like(SimMsgCodeMappingEntity::getReqMsgName, msgCodeMapping.getReqMsgName());
        }
        if (StringUtils.hasLength(msgCodeMapping.getRespChannelNo())) {
            wrapper.eq(SimMsgCodeMappingEntity::getRespChannelNo, msgCodeMapping.getRespChannelNo());
        }
        if (StringUtils.hasLength(msgCodeMapping.getRespMsgName())) {
            wrapper.like(SimMsgCodeMappingEntity::getRespMsgName, msgCodeMapping.getRespMsgName());
        }
        page(page, wrapper);

        List<SimMsgCodeMappingEntity> recordList = page.getRecords();
        List<SimMsgCodeMapping> list = new ArrayList<>(recordList.size());
        for (SimMsgCodeMappingEntity msgCodeMappingEntity : recordList) {
            SimMsgCodeMapping simMsgCodeMapping = new SimMsgCodeMapping();
            BeanUtils.copyProperties(msgCodeMappingEntity, simMsgCodeMapping);
            list.add(simMsgCodeMapping);
        }
        page.setRecords(list);

        return page;
    }

}
